import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'

Vue.use(Router)

/* Layout */
// import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
 roles: ['admin','editor']    control the page roles (you can set multiple roles)
 title: 'title'               the name show in sidebar and breadcrumb (recommend set)
 icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
 breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
 activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
 }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [{
  path: '/',
  name: 'login',
  component: lazyLoadView(() => import('@/views/login/index')),
  meta: {
    keepAlive: false,
    noCache: true,
    title: '登录',
    icon: 'dashboard'
  },
  hidden: true,
  unique: false
}, {
  path: '/login',
  name: 'login',
  component: lazyLoadView(() => import('@/views/login/index')),
  meta: {
    keepAlive: false,
    noCache: true,
    title: '登录',
    icon: 'dashboard'
  },
  hidden: true,
  unique: false
}, {
  path: '/v2/1/login',
  name: 'login',
  component: lazyLoadView(() => import('@/views/login/index')),
  meta: {
    keepAlive: false,
    noCache: true,
    title: '登录',
    icon: 'dashboard'
  },
  hidden: true,
  unique: false
}, {
  path: '/v2/2/login',
  name: 'login',
  component: lazyLoadView(() => import('@/views/login/index')),
  meta: {
    keepAlive: false,
    noCache: true,
    title: '登录',
    icon: 'dashboard'
  },
  hidden: true,
  unique: false
}, {
  path: '/homepage',
  name: 'homepage',
  component: lazyLoadView(() => import('@/views/homepage/index')),
  meta: {
    keepAlive: false,
    noCache: true,
    title: '主页',
    icon: 'shouye'
  },
  hidden: true,
  unique: false
}, {
  path: '/main',
  name: 'main',
  component: lazyLoadView(() => import('@/views/main/index')),
  meta: {
    noCache: true,
    title: '首页',
    icon: 'shouye',
    permissionCode: '00'
  },
  hidden: false,
  unique: false,
  disableDelete: false
}, {
  path: '/userCenter/userInfo',
  name: 'userInfo',
  component: lazyLoadView(() => import('@/views/userCenter/userInfo')),
  meta: {
    noCache: true,
    title: '个人信息',
    icon: '',
    permissionCode: '90'
  },
  hidden: true,
  unique: false,
  disableDelete: false
}, {
  path: '/userCenter/updatePassword',
  name: 'updatePassword',
  component: lazyLoadView(() => import('@/views/userCenter/updatePassword')),
  meta: {
    noCache: true,
    title: '修改密码',
    icon: '',
    permissionCode: '00'
  },
  hidden: true,
  unique: false,
  disableDelete: false
}, {
  path: '/member',
  name: 'member',
  meta: {
    noCache: true,
    title: '会员管理',
    icon: 'form',
    permissionCode: '11'
  },
  unique: false,
  hidden: false
}, {
  path: '/member/info/index',
  name: 'memberInfoIndex',
  component: lazyLoadView(() => import('@/views/member/info/index.vue')),
  meta: {
    noCache: true,
    title: '会员信息',
    icon: '',
    permissionCode: '1101'
  },
  unique: false,
  hidden: false
},
// {
//   path: '/member/info1/index',
//   name: 'memberInfo1Index',
//   component: lazyLoadView(() => import('@/views/member/info1/index.vue')),
//   meta: {
//     noCache: true,
//     title: '会员信息1',
//     icon: '',
//     permissionCode: '1101'
//   },
//   unique: false,
//   hidden: false
// },
{
  path: '/member/benefitCard/index',
  name: 'memberBenefitCardIndex',
  component: lazyLoadView(() => import('@/views/member/benefitCard/index.vue')),
  meta: {
    noCache: true,
    title: '权益卡',
    icon: '',
    permissionCode: '1102'
  },
  unique: false,
  hidden: false
}, {
  path: '/member/benefitCard/form',
  name: 'memberBenefitCardForm',
  component: lazyLoadView(() => import('@/views/member/benefitCard/form.vue')),
  meta: {
    noCache: true,
    title: '添加修改权益卡',
    icon: '',
    permissionCode: '1102'
  },
  unique: false,
  hidden: true
}, {
  path: '/member/membership/index',
  name: 'memberMembershipIndex',
  component: lazyLoadView(() => import('@/views/member/membership/index.vue')),
  meta: {
    noCache: true,
    title: '易购卡',
    icon: '',
    permissionCode: '1102'
  },
  unique: false,
  hidden: false
},{
  path: '/member/rechargeOrder/index',
  name: 'memberRechargeOrderIndex',
  component: lazyLoadView(() => import('@/views/member/rechargeOrder/index.vue')),
  meta: {
    noCache: true,
    title: '充值订单',
    icon: '',
    permissionCode: '1102'
  },
  unique: false,
  hidden: false
},{
  path: '/member/rechargeActivity/index',
  name: 'memberRechargeActivityIndex',
  component: lazyLoadView(() => import('@/views/member/rechargeActivity/index.vue')),
  meta: {
    noCache: true,
    title: '充值活动',
    icon: '',
    permissionCode: '1102'
  },
  unique: false,
  hidden: false
},{
  path: '/member/rechargeActivity/detail',
  name: 'memberrechargeActivityDetail',
  component: lazyLoadView(() => import('@/views/member/rechargeActivity/detail.vue')),
  meta: {
    noCache: true,
    title: '充值活动详情',
    icon: '',
    permissionCode: '1102'
  },
  unique: false,
  hidden: true
},

{
  path: '/member/membershipDetails/index',
  name: 'memberMembershipDetailsIndex',
  component: lazyLoadView(() => import('@/views/member/membershipDetails/index.vue')),
  meta: {
    noCache: true,
    title: '易购卡余额明细',
    icon: '',
    permissionCode: '1102'
  },
  unique: false,
  hidden: false
},
// 404 page must be placed at the end !!!
{
  path: '*',
  redirect: '/404',
  hidden: true
}
]

function lazyLoadView(AsyncView) {
  return () => {
    return AsyncView().catch(error => {
      window.parent.postMessage({
        cmd: 'hasNewVersion',
        params: ''
      })
      console.error('模块文件加载失败:', error)
      // 处理错误，比如跳转到 404 页面
      // router.push({ name: 'Error404' });
    })
  }
}

const createRouter = () => {
  const _router = new Router({
    // mode: 'history', // require service support
    mode: 'hash', // require service support
    // scrollBehavior: () => ({y: 0}),
    routes: constantRoutes
  })
  // _router.beforeResolve((to, from, next) => {
  //   // 检查js文件是否存在
  //   next();
  // })
  return _router
}

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
